package cn.cjx.basic.controller;

import cn.cjx.basic.dto.LoginDto;
import cn.cjx.basic.dto.WechatDto;
import cn.cjx.basic.exception.BusinessException;
import cn.cjx.basic.service.ILoginService;
import cn.cjx.basic.utils.AjaxResult;
import cn.cjx.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author: cjx
 * @Date: 2022/5/10
 */
@RestController
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private ILoginService loginService;


	@PostMapping("/wechat")
	public AjaxResult wechatLogin(@RequestBody WechatDto dto) {

		try {
			return loginService.wechatLogin(dto);
		}catch (BusinessException e) {//我们自己的业务错误  自定义异常
			e.printStackTrace();
			return AjaxResult.error().setMessage(e.getMessage());
		}catch (Exception e) {//系统级别的错误
			e.printStackTrace();
			return AjaxResult.error();
		}

	}


	@PostMapping("/wechat/binder")
	public AjaxResult wechatBinder(@RequestBody WechatDto dto) {

		try {
			HashMap<String, Object> map = loginService.wechatBinder(dto);
			return AjaxResult.success().setResultObj(map);
		}catch (BusinessException e) {//我们自己的业务错误  自定义异常
			e.printStackTrace();
			return AjaxResult.error().setMessage(e.getMessage());
		}catch (Exception e) {//系统级别的错误
			e.printStackTrace();
			return AjaxResult.error();
		}

	}

	@PostMapping("/account")
	public AjaxResult loginAccount(@RequestBody LoginDto dto) {

		try {
			HashMap<String, Object> map = loginService.loginAccount(dto);
			return AjaxResult.success().setResultObj(map);
		}catch (BusinessException e) {//我们自己的业务错误  自定义异常
			e.printStackTrace();
			return AjaxResult.error().setMessage(e.getMessage());
		}catch (Exception e) {//系统级别的错误
			e.printStackTrace();
			return AjaxResult.error();
		}

	}
}
